package com.qlh.camera

import java.awt.Frame
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import java.io.File
import javax.swing.*

object CommonUtil {

    /**
     * 程序关闭
     * **/
    fun windowsClose(listener: ConfirmCallback) {
        val title = "系统提示"
        val msg = "确定关闭窗口，退出程序吗？"
        val option = JOptionPane.YES_NO_OPTION
        println("准备关闭程序？")
        val buttonValue = JOptionPane.showConfirmDialog(null, msg, title, option)
        if (buttonValue == JOptionPane.YES_OPTION) {
            listener.confirm()
        } else {
            println("取消关闭")
        }
    }

    fun showDlgAndInput(frame: Frame) {
        val result = arrayListOf<String>()
        val config = FileUtil.getDefaultParams()
        val ipLabel = JLabel("IP地址")
        val ipTextField = JTextField().apply {
            config["ip"]?.let {
                text = it
            }
        }

        val userLabel = JLabel("用户名")
        val userTextField = JTextField().apply {
            config["name"]?.let {
                text = it
            }
        }

        val pwdLabel = JLabel("密码")
        val pwdTextField = JTextField().apply {
            config["pwd"]?.let {
                text = it
            }
        }

        val portLabel = JLabel("端口")
        val portTextField = JTextField().apply {
            config["port"]?.let {
                text = it
            }
        }

        //创建dlg
        val dlg = JDialog(frame, "参数设置", true).apply {
            setSize(300, 300)
        }

        val saveBtn = JButton("保存设置").apply {
            addMouseListener(object : MouseAdapter() {
                override fun mousePressed(e: MouseEvent?) {
                    if (ipTextField.text.isEmpty() || userTextField.text.isEmpty() ||
                            pwdTextField.text.isEmpty() || portTextField.text.isEmpty()) {
                        JOptionPane.showMessageDialog(frame,"请填写完整","提示",JOptionPane.WARNING_MESSAGE)
                        return
                    }
                    FileUtil.saveDefaultParams(ipTextField.text, userTextField.text, pwdTextField.text, portTextField.text)
                    //关闭设置对话框
                    dlg.isVisible = false
                }
            })
        }

        val jPanelSettingParams = JPanel()
        val jPanelUserInfoLayout = GroupLayout(jPanelSettingParams)
        jPanelSettingParams.layout = jPanelUserInfoLayout
        jPanelUserInfoLayout.setHorizontalGroup(
                jPanelUserInfoLayout.createParallelGroup(GroupLayout.Alignment.CENTER)
                        .addGroup(
                                jPanelUserInfoLayout.createSequentialGroup() //IP
                                        .addView(ipLabel, ipTextField)
                        )
                        .addGroup( //创建串行组
                                jPanelUserInfoLayout.createSequentialGroup() //用户名
                                        .addView(userLabel, userTextField)
                        )
                        .addGroup(
                                jPanelUserInfoLayout.createSequentialGroup() //密码
                                        .addView(pwdLabel, pwdTextField, b2 = 30)
                        )
                        .addGroup(
                                jPanelUserInfoLayout.createSequentialGroup()//端口
                                        .addView(portLabel, portTextField, b2 = 30)
                        )
                        .addComponent(saveBtn, GroupLayout.Alignment.CENTER)
        )
        jPanelUserInfoLayout.setVerticalGroup(
                jPanelUserInfoLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(
                                jPanelUserInfoLayout.createSequentialGroup()
                                        .addGap(9, 9, 9)
                                        .addView(jPanelUserInfoLayout, ipLabel, ipTextField)
                                        .addGap(9, 9, 9)
                                        .addView(jPanelUserInfoLayout, userLabel, userTextField)
                                        .addGap(9, 9, 9)
                                        .addView(jPanelUserInfoLayout, pwdLabel, pwdTextField)
                                        .addGap(9, 9, 9)
                                        .addView(jPanelUserInfoLayout, portLabel, portTextField)
                                        .addGap(9, 19, 30)
                                        .addComponent(saveBtn)
                                        .addGap(9, 9, 9)
                        )
        )
        dlg.contentPane = jPanelSettingParams
        dlg.setLocationRelativeTo(frame)


        //显示对话框（setVisible()方法会阻塞，直到对话框关闭）

        dlg.isVisible = true
    }

}